#include "searchwindow.h"

void SearchWindow::init()
{
    setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);      //浮动于窗口上方
    setWindowFlags(windowFlags()&~Qt::WindowMinMaxButtonsHint);    //隐藏最大化按钮
    setFixedSize(500, 150);                 //禁止拖动窗口大小
    //setAttribute (Qt::WA_DeleteOnClose);    //关闭后销毁窗口

    toolbar = new QToolBar(this);
    statusbar = new QStatusBar(this);       //状态栏
    count_label = new QLabel(this);
    count_label -> setText("共 0 个, 第 0 个");
    statusbar -> addWidget(count_label);
    statusbar -> move(0, 115);

    count_label = new QLabel(this);
    find = new QAction(this);           //设置搜索按钮和替换按钮
    find -> setText("查找");
    replace = new QAction(this);
    replace -> setText("替换");
    toolbar -> addAction(find);
    toolbar -> addAction(replace);

    prompt1 = new QLabel(this);             //设置搜索引导语和替换引导语
    prompt2 = new QLabel(this);
    prompt1 -> setText("查找内容：");
    prompt1 -> move(0, 70);
    prompt2 -> setText("替换为：");
    prompt2 -> move(0, 155);
    findtext = new QLineEdit(this);         //设置搜索文本框
    findtext -> resize(400, 50);
    findtext -> move(75, 50);
    replacetext = new QLineEdit(this);      //设置替换文本框
    replacetext -> resize(400, 50);
    replacetext -> move(75, 135);
    to_find_pre = new QPushButton(this);    //设置点击搜索
    to_find_pre -> setText("搜索上一处");
    to_find_pre -> move(260, 110);
    to_find_next = new QPushButton(this);   //设置点击搜索
    to_find_next -> setText("搜索下一处");
    to_find_next -> move(380, 110);
    to_replace = new QPushButton(this);     //设置点击替换
    to_replace -> setText("替换");
    to_replace -> move(200, 200);

    prompt2 -> close();                     //默认是搜索页面
    replacetext -> close();
    to_replace -> close();
}

void SearchWindow::connect_set()
{
    connect(find, &QAction::triggered, this, &SearchWindow::find_widget);                       //点击搜索按钮，进入搜索页面
    connect(replace, &QAction::triggered, this, &SearchWindow::replace_widget);                 //点击替换按钮，进入替换页面
    connect(to_find_pre, &QPushButton::pressed, this, &SearchWindow::send_find_pre);            //点击搜索前一个，向主页面发送搜索信号
    connect(to_find_next, &QPushButton::pressed, this, &SearchWindow::send_find_next);          //点击搜索下一个，向主页面发送搜索信号
    connect(to_replace, &QPushButton::pressed, this, &SearchWindow::send_replace);              //点击替换按钮
}

SearchWindow::SearchWindow(QWidget *parent) : QWidget(parent)
{
    init();
    connect_set();
}

void SearchWindow::find_widget()
{
    setFixedSize(500, 150);
    prompt2 -> close();                     //撤去替换页面内容
    replacetext -> close();
    to_replace -> close();
    to_find_pre -> show();                  //点击搜索复原
    to_find_next -> show();
    statusbar -> show();
}

void SearchWindow::replace_widget()
{
    setFixedSize(500, 250);
    prompt2 -> show();                      //增添替换页面内容
    replacetext -> show();
    to_replace -> show();
    to_find_next -> close();
    to_find_pre -> close();
    statusbar -> close();
}

void SearchWindow::send_find_pre()           //发送搜索信号
{
    emit find_signal(0);
}

void SearchWindow::send_find_next()
{
    emit find_signal(1);
}

void SearchWindow::send_replace()
{
    emit replace_signal(findtext -> text(), replacetext -> text());
}

void SearchWindow::update_count(int new_count_find)
{
    count_find = new_count_find;
}

QString SearchWindow::getFindText()     //获得搜索文本框内容
{
    return findtext -> text();
}

QString SearchWindow::getReplaceText()
{
    return replacetext -> text();
}
